package com.diaosidadui.demo;

import java.util.Random;
import java.util.Scanner;

public class GuessNumber {
    public static void main(String[] args) {
        Random r = new Random();
        Scanner sc = new Scanner(System.in);
        //获取随机数
        int num = r.nextInt(100)+1;
        //输入猜的数字
        System.out.println("猜1~100之间的数字");
        int count =0;

        while (true) {
            count++;
            System.out.println("请输入:");
            int guess = sc.nextInt();
            //随机数和猜之间比较
            if(guess>num){
                System.out.println("猜大了");
                if(count==3){
                    System.out.println("机会用完了");
                }else{
                    System.out.println("还有"+(3-count)+"次机会");
                }

            }else if(guess<num){
                System.out.println("猜小了");
                if(count==3){
                    System.out.println("机会用完了");
                }else{
                    System.out.println("还有"+(3-count)+"次机会");
                }

            }else{
                System.out.println("猜中了");
                break;
            }
            if(count==3){
                break;
            }


        }
    }
}
